__author__ = 'Douglas'


class HebRule:
    def __init__(self):
        self.w1 = 1
        self.w2 = -1
        self.rate = 1.0
        self.n_epoch = 1

    def epoch(self):
        print("***Beginning Epoch #", self.n_epoch, "***")
        self.present_pattern(-1, -1)
        self.present_pattern(-1, 1)
        self.present_pattern(1, -1)
        self.present_pattern(1, 1)
        self.n_epoch += 1

    def present_pattern(self, a, b):
        print("Presented [", a, ",", b, "]")
        result = self.recognize(a, b)
        print("Result =", result)

        # adjust weight 1
        delta = self.training_function(a, result)
        self.w1 += delta
        print("Delta w1 =", delta)

        # adjust weight 2
        delta = self.training_function(b, result)
        self.w2 += delta
        print("Delta w2 =", delta)

    def recognize(self, a, b):
        return ((self.w1 * a) + (self.w2 * b)) * .5

    def training_function(self, i, o):
        return self.rate * i * o

    def run(self):
        for i in range(0, 5):
            self.epoch()
